package me.obstsalatt.guildera2.utility;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: Robin
 * Date: 14.02.14, 00:24
 * Project: GuildEra2
 * Package: me.obstsalatt.guildera2
 * Descriprion:
 * something
 */
public class Util {
    public static String readFile(String pathname) throws IOException {

        File file = new File(pathname);
        if(!file.exists()) { return ""; }
        StringBuilder fileContents = new StringBuilder((int)file.length());
        Scanner scanner = new Scanner(file);
        String lineSeparator = System.getProperty("line.separator");

        try {
            while(scanner.hasNextLine()) {
                fileContents.append(scanner.nextLine() + lineSeparator);
            }
            return fileContents.toString();
        } finally {
            scanner.close();
        }
    }

    public static void writeFile(String pathname, String content) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter( new FileWriter(pathname));
            writer.write(content);
        }
        catch (IOException e) { }
        finally {
            try {
                if ( writer != null)
                    writer.close();
            }
            catch (IOException e) { }
        }
    }
}
